<template>
  <div class='container'>
    <el-dialog
  title="提示"
  :visible="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <el-form :model="info" ref="form" :rules="rules" label-width="100px" class="demo-ruleForm">
  <el-form-item label="所属学科">
    <el-input v-model.number="info.subjectID" ></el-input>
  </el-form-item>
  <el-form-item label="目录名称">
    <el-input v-model="info.directoryName"></el-input>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="submit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    addInfo: {
      type: Object
    },
    type: {
      type: String,
      default: 'add'
    }
  },
  data () {
    return {
      rules: {},
      info: {
        id: null,
        directoryName: '',
        subjectID: 132,
        subjectName: null
      }
    }
  },
  methods: {
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    async close () {
      this.$emit('update:dialogVisible', false)
      this.$emit('update:addInfo', {})
    },
    async submit () {
      if (this.type === 'add') {
        await add(this.info)
      } else if (this.type === 'change') {
        await update(this.info)
        this.$emit('renderlist')
      }
      this.close()
    }

  },
  watch: {
    dialogVisible (newvalue, old) {
      // console.log(old, newvalue)
      if (this.type === 'change') {
        this.info.subjectName = this.addInfo.subjectName
        this.info.directoryName = this.addInfo.directoryName
        this.info.id = this.addInfo.id
        this.info.subjectID = this.addInfo.subjectID
        // console.log(this.info)
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
